<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
/**
 * 管理员管理
 * @author fld
 *
 */
class AdminController extends BaseController{

	function __construct(){
		parent::__construct();
		$this->db = model('Admin');
	}
	/**
	 * 管理员列表
	 */
	public function indexOp(){
		$result = $this->getCondition();
		$list = $this->db->getAdminList($result);
		$this->assign("list",$list);
		// 获取分页显示
		$page = $list->render();
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	public function getCondition(){
		$result = array();
		$condition = array();
		$query = array();
		//关键字
		$keyword = input('param.keyword/s');
		if(!empty($keyword)){
			$condition['admin_name|admin_truename'] = array('like','%'.$keyword.'%');
			$query['keyword'] = $keyword;
		}
		$this->assign('keyword',$keyword);
		$result['condition'] = $condition;
		$result['query'] = $query;
		return $result;
	}
	/**
	 * 添加管理员信息
	 */
	public function addOp(){
		$model = model('base');
	    if (chksubmit()){
	    	$res = $this->db->addData();
	        if($res !== false){
	            $this->msg = "添加管理员成功";
	            $this->success($this->msg,url('admin/index'));
	        }else{
	            $this->msg = "添加管理员失败";
	            $this->error($this->msg);
	        }
	    }else{
	    	//角色列表
	        $role_list = model('Role')->getRoles();
	        $this->assign('role_list',$role_list);
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑管理员信息
	 */
	public function editOp(){
		$model = model('base');
		if (chksubmit()){
			$res = $this->db->updateData();
			if($res !== false){
				$this->msg = "编辑管理员成功";
				$result['error'] = 0;
			}else{
				$this->msg = "编辑管理员失败";
				$result['error'] = 1;
			}
			$result['msg'] = $this->msg;
			return json($result);
		}else{
	    	//角色列表
	        $role_list = model('Role')->getRoles();
	        $this->assign('role_list',$role_list);
			//管理员信息
			$admin_id = input('param.admin_id');
			$admin_info = $this->db->getAdminInfo(array('admin_id'=>$admin_id));
			$this->assign('vo',$admin_info);
			return $this->fetch();
		}
	}
	/**
	 * 删除管理员信息
	 */
	public function deleteOp(){
		$result = array();
		$res = $this->db->deleteData();
		if($res !== false){
			$result['error'] = 0;
		}else{
			$this->msg = "删除管理员失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
	
	public function resetPwdOp(){
		$model = model('base');
		if (chksubmit()){
			$res = $this->db->resetPwd();;
			if($res !== false){
				$this->msg = "编辑管理员密码成功";
				$result['error'] = 0;
			}else{
				$this->msg = "编辑管理员密码失败";
				$result['error'] = 1;
			}
			$result['msg'] = $this->msg;
			return json($result);
		}else{
			//管理员信息
			$admin_id = input('param.admin_id');
			$admin_info = $this->db->getAdminInfo(array('admin_id'=>$admin_id));
			$this->assign('vo',$admin_info);
			return $this->fetch();
		}
	}
	
	/**
	 * 检查用户名是否存在
	 */
	public function check_ajaxOp(){
		$admin_name = input('post.admin_name');
		$admin_info = $this->db->getAdminInfo(array('admin_name'=>$admin_name));
		if($admin_info){
			return json(false);
		}else{
			return json(true);
		}
	}
	/**
	 * 验证密码是否错误
	 */
	public function checkPwd_ajaxOp(){
		$old_pwd = input('post.old_pwd');
		$admin_id = input('post.admin_id');
		$model = model('base');
		$admin_info = $this->db->getAdminInfo(array('admin_id'=>$admin_id));
		if($admin_info){
			if($admin_info['admin_password'] == encrypt($old_pwd)){
				return json(true);
			}else{
				return json(false);
			}
		}else{
			return json(false);
		}
	}
	
	/**
	 * 查看管理员信息
	 */
	public function viewOp(){
		$model = model('base');
		//角色列表
		$role_list = model('Role')->getRoles();
		$this->assign('role_list',$role_list);
		//管理员信息
		$admin_id = session("admin_id");
		$admin_info = $this->db->getAdminInfo(array('admin_id'=>$admin_id));
		$this->assign('vo',$admin_info);
		return $this->fetch();
	}
}